package com.sunricher.telinkblemeshlib;

import android.util.Log;

import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleScanCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.data.BleScanState;
import com.clj.fastble.scan.BleScanRuleConfig;

import java.util.List;

public class SensorManager {

    public static abstract class Callback {
        /**
         *
         * @deprecated use `didDiscoverSingleSensor` instead of it
         */
        @Deprecated
        public abstract void didDiscoverSensor(MeshNode node);

        public abstract void didDiscoverSingleSensor(MeshNode sensor, byte sensorType);

        public abstract void didDiscoverUniversalRemote(MeshNode node);
    }

    private Callback callback;

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    private final BleScanCallback scanCallback = new BleScanCallback() {
        @Override
        public void onScanFinished(List<BleDevice> scanResultList) {
            Log.i("SensorManager", "onScanFinished");
        }

        @Override
        public void onScanStarted(boolean success) {
            Log.i("SensorManager", "onScanStarted");
        }

        @Override
        public void onScanning(BleDevice bleDevice) {
        }

        @Override
        public void onLeScan(BleDevice bleDevice) {
            super.onLeScan(bleDevice);
            MeshNode node = MeshNode.make(bleDevice);
            if (node == null) {
                return;
            }
            if (callback == null) {
                return;
            }
            if (node.getDeviceType().getCategory() == MeshDeviceType.Category.sensor) {
                callback.didDiscoverSensor(node);
                byte sensorType = 0x00;
                switch (node.getDeviceType().getRawValue2()) {
                    // todo: do the next step
                    case 0x01:
                        sensorType = MeshCommand.SingleSensorAction.SensorType.doorContactSensor;
                        break;
                    case 0x02:
                        sensorType = MeshCommand.SingleSensorAction.SensorType.waterLeakSensor;
                        break;
                    default:
                        break;
                }
                callback.didDiscoverSingleSensor(node, sensorType);
            } else if (node.getDeviceType().getCategory() == MeshDeviceType.Category.universalRemote) {
                callback.didDiscoverUniversalRemote(node);
            }
        }
    };

    private SensorManager() {
    }

    public static SensorManager getInstance() {
        return SingletonHolder.instance;
    }

    public void scanSensor() {
        if (!BleManager.getInstance().isBlueEnable()) {
            return;
        }
        BleScanRuleConfig.Builder builder = new BleScanRuleConfig.Builder();
        builder.setScanTimeOut(0);
        BleScanRuleConfig config = builder.build();
        BleManager.getInstance().initScanRule(config);
        BleManager.getInstance().scan(scanCallback);
    }

    public void stopScan() {
        if (!BleManager.getInstance().isBlueEnable()) {
            return;
        }
        if (BleManager.getInstance().getScanSate() == BleScanState.STATE_SCANNING) {
            BleManager.getInstance().cancelScan();
        }
    }

    private static class SingletonHolder {
        private static final SensorManager instance = new SensorManager();
    }
}
